package cc.customer.common.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Utils {
    /**获取UUID 做为数据库各个表的主键,不采用自增长*/
    public static String getUUID (){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        return uuid;
    }
    /**生成导出批次**/
    public static String getExportBatchName(String num){
        try {
            Date date = new Date();
            return "E"+DateUtil.getYear(date)+DateUtil.getMonth(date)+DateUtil.getDay(date)+"一共"+num+"份";
        }catch (Exception e){

        }
        return "未知";
    }

    public static JSONObject getJSONParam(HttpServletRequest request){
        JSONObject jsonParam = null;
        try {
            // 获取输入流
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

            // 写入数据到Stringbuilder
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = streamReader.readLine()) != null) {
                sb.append(line);
            }
            jsonParam = JSONObject.parseObject(sb.toString());
            // 直接将json信息打印出来
            //System.out.println(jsonParam.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonParam;
    }

    /**格式化星期**/
    public static String formatterWeek(String week){
        String formatter  = "";
        try {
            switch (week){
                case "1":
                    formatter =  "一";
                    break;
                case "2":
                    formatter = "二";
                    break;
                case "3":
                    formatter = "三";
                    break;
                case "4":
                    formatter = "四";
                    break;
                case "5":
                    formatter = "五";
                    break;
                case "6":
                    formatter = "六";
                    break;
                default:
                    formatter = "日";

            }

        }catch (Exception e){

        }
        return formatter;
    }

    /**差分平均list 集合**/

    public static <T> List<List<T>> averageAssign(List<T> source, int limit) {
        if (null == source || source.isEmpty()) {
            return Collections.emptyList();
        }
        List<List<T>> result = new ArrayList<>();
        int listCount = (source.size() - 1) / limit + 1;
        int remaider = source.size() % listCount; // (先计算出余数)
        int number = source.size() / listCount; // 然后是商
        int offset = 0;// 偏移量
        for (int i = 0; i < listCount; i++) {
            List<T> value;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }
}
